package com.study.web.controller.message;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.study.app.domain.bo.AppUserInfoSetBo;
import com.study.app.domain.vo.AppUserInfoSetVo;
import com.study.app.domain.vo.AppUserVo;
import com.study.app.service.IAppUserInfoSetService;
import com.study.app.service.IAppUserService;
import com.study.common.annotation.Log;
import com.study.common.annotation.RepeatSubmit;
import com.study.common.core.controller.AppBaseController;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.domain.model.AppLoginUser;
import com.study.common.core.domain.model.LoginUser;
import com.study.common.core.page.TableDataInfo;
import com.study.common.core.validate.AddGroup;
import com.study.common.core.validate.EditGroup;
import com.study.common.enums.BusinessType;
import com.study.common.helper.LoginHelper;
import com.study.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
/**
 * App用户消息设置
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/message")
public class MessageSettingsController extends AppBaseController {

    private final IAppUserInfoSetService iAppUserInfoSetService;

    /**
     * 查询用户消息设置列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<AppUserInfoSetVo> list(AppUserInfoSetBo bo, PageQuery pageQuery) {
        return iAppUserInfoSetService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户消息设置列表
     */
    @SaCheckLogin
    @Log(title = "用户消息设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserInfoSetBo bo, HttpServletResponse response) {
        List<AppUserInfoSetVo> list = iAppUserInfoSetService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户消息设置", AppUserInfoSetVo.class, response);
    }

    /**
     * 获取用户消息设置详细信息
     *
     */
    @SaCheckLogin
    @GetMapping()
    public R<AppUserInfoSetVo> getInfo() {
        return R.ok(iAppUserInfoSetService.queryByUserId(getUserId()));
    }

    /**
     * 新增用户消息设置
     */
    @SaCheckLogin
    @Log(title = "用户消息设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserInfoSetBo bo) {

        return toAjax(iAppUserInfoSetService.insertByBo(bo));
    }

    /**
     * 修改用户消息设置
     */
    @SaCheckLogin
    @Log(title = "用户消息设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserInfoSetBo bo) {
        return toAjax(iAppUserInfoSetService.updateByBo(bo));
    }

    /**
     * 删除用户消息设置
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "用户消息设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppUserInfoSetService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
